#ifndef FITNESS_REAL_CU
#define FITNESS_REAL_CU

#include <math.h>

#include "fitnessBinaria.cuh"
#include "../utils/global.cuh"


float calcularFitnessRealCPU(float* poblacion, int posicion){
	float fitness = 0;
	float aux = 418.9829 * cantGenesEnCromosoma;
	for(int i = 0; i < cantGenesEnCromosoma; i++){
	  fitness = fitness + poblacion[posicion + i] * sin(sqrt(fabs(poblacion[posicion + i])));
	}
	return fitness - aux;
}

__device__ float calcularFitnessRealGPU(float* poblacion, int posicion){
	float fitness = 0;
	float aux = 418.9829 * cantGenesEnCromosomaDevice;
	for(int i = 0; i < cantGenesEnCromosomaDevice; i++){
	  fitness = fitness + poblacion[posicion + i] * sin(sqrt(fabs(poblacion[posicion + i])));
	}
	return fitness - aux;
}

__global__ void calcularFitnessRealGPUGen(float* poblacion, float* vectorFitness, float* fitnessOrdenadoDevice){
	int posicionIndividuo = blockIdx.y * cantGenesEnCromosomaDevice;
	int posicionFitness = blockIdx.y;
	float fitness = 0;
	for(int i = posicionIndividuo + threadIdx.y; i < posicionIndividuo + cantGenesEnCromosomaDevice; i = i + cantHilosBloqueDevice){
		fitness = fitness + poblacion[i] * sin(sqrt(fabs(poblacion[i])));
	}
	atomicAdd(&(vectorFitness[posicionFitness]), fitness - (418.9829 * cantGenesEnCromosomaDevice / cantHilosBloqueDevice));
	fitnessOrdenadoDevice[posicionFitness] = vectorFitness[posicionFitness];
}

#endif

